<?php
require 'db.php';
$malop=isset($_REQUEST['malop'])?$_REQUEST['malop']:'';
$sql = "SELECT count(*) FROM dbo_sinhvien";
$sql .= " WHERE Malop=?";
$stmt = $db->prepare($sql);

$stmt->bindParam(1,$malop,PDO::PARAM_STR);

$stmt->execute();

$row = $stmt->fetch();

// tong so dong co trong CSDL
$totalRow = $row[0];

// so dong tren moi trang
$limit = 10;

// tinh tong so trang
$total_page = $totalRow==0?1:ceil($totalRow/$limit);


// lan dau goi trang nen trang hien tai = 1
$current_page = 1;
	
// neu gia tri trang moi duoc submit
if (isset($_REQUEST['page'])){
	$current_page = $_REQUEST['page'] ;
}

// so trang hien thi cho chon; vi du << 1 | 2 | 3 | 4 | 5 | >>
$nr_page = 5;

// doan hien tai
$segment = 1;

if (isset($_POST['segment'])){
	$segment = $_POST['segment'];
}


// neu doan chon ke tiep
if (isset($_POST['next'])){
	$segment += 1;
}

// neu chon doan truoc do
if (isset($_POST['previous'])){
	$segment -= 1;
}



$begin = $segment==1?1:($segment-1)*$nr_page+1;

$end = ($begin+$nr_page-1)<$total_page?$begin+$nr_page-1:$total_page;
$current_page = $current_page<$begin?$begin:$current_page;



	// tinh vi tri bat dau select
$start = $current_page==1?0:($current_page-1)*$limit;

$sql = "SELECT MaSV, Holot, Ten, NgaySinh, Email, QueQuan FROM dbo_sinhvien";
$sql .= " WHERE Malop=? LIMIT $start,$limit";
	

$stmt = $db->prepare($sql);

$stmt->bindParam(1,$malop,PDO::PARAM_STR);

$stmt->execute();

?>

		<table id="dssv" class="table table-bordered table-hover">
			<thead>
			<tr>
			<td colspan="7">
			<a href="#" class="btn btn-default" id="btnAdd"><span class="glyphicon glyphicon-plus">Thêm</span></a>
			</td>
			</tr>
			<tr class="info"><th>STT</th><th>MSSV</th><th>Họ lót</th><th>Tên</th><th>Ngày sinh</th><th></th><th></th></tr></thead>
			<tbody>
		<?php $i=1;
	
		if($stmt->execute()){
			
				while($r=$stmt->fetch()){?>
					<?php 	if($i%2==0){echo '<tr class="active">';}else {echo '<tr></tr>';}?>
					
					<td><?php echo $i++;?></td>
					<td><?php echo $r['MaSV'];?></td>
					<td><?php echo $r['Holot'];?></td>
					<td><?php echo $r['Ten'];?></td>
					<td><?php echo $r['NgaySinh'];?></td>
					<td><a class="edit" id="btnedit" href='#' data-mssv=<?php echo $r['MaSV']?>><span class="glyphicon glyphicon-pencil"></span></a></td>
					<td><a class="del" id="btndel" href='#' data-mssv=<?php echo $r['MaSV']?>><span class="glyphicon glyphicon-trash"></span></a></td>
		<?php }} ?>	
			</tbody>
<tfoot>
		 		<tr>
			<td colspan="7">
					 
		 			
		 			<ul class="pagination">
		 				<li class="<?php echo $current_page==1?'disabled':'';?>" >
		 					<a href="#" id="previous" 
		 						data-segment="<?php echo $segment==1?1:($segment-1); ?>" 
		 						data-page="<?php echo $end-$nr_page;?>">&laquo;</a></li>
		 				 
		 				 <?php for($i=$begin;$i<=$end;$i++):?>
		 				 	<li class="<?php echo $i==$current_page?'active':'';?>"><a href="#" class="page_number" 
		 				 		data-segment="<?php echo $segment; ?>" 
		 				 		data-page="<?php echo $i;?>"><?php echo $i;?> </a></li>
		 				 <?php endfor;?>
		 				 
		 				<li class="<?php echo $end==$total_page?'disabled':'';?>">
		 					<a href="#" id="next" 
		 						data-segment="<?php echo $segment+1; ?>" 
		 						data-page="<?php echo $end+1 ?>">&raquo;</a></li>
		 			</ul> 
			</td>
		</tr>
		 	</tfoot>
		</table>
